﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using DBScript;
using eBlog.Common;
using eBlog.Entity;
using eBlog.Core;
using eBlog.Common.Security;

namespace eBlog.Web.Admin
{
    public partial class _Main : PageBase
    {
        IDBBase dal = DBFactory.GetDbProvider(); //初始化数据工厂

        protected string RoleName;
        protected string AdminName;
        protected string IP;
        protected string Time;
        protected string LoginCount;

        protected string Pnum0;
        protected string Pnum;

        protected string Tnum;
        protected string Tnum0;

        protected string DbType;

        

        protected void Page_Load(object sender, EventArgs e)
        {

            PageValidate.CheckAdminLogin(this);


            //string uuu = HttpContext.Current.Items["DBS.CURRENT.DBTYPE"].ToString();

            int CurrUID = 2;// int.Parse(Request.Cookies["eBlogAdmin"].Values["id"]);

            this.txtAdminName.Value ="BangQ-txt"; //Server.UrlDecode(HttpContext.Current.Request.Cookies["eBlogAdmin"].Values["adminname"]);
            this.txtLoginName.Value = "BangQ-txt"; //HttpContext.Current.Request.Cookies["eBlogAdmin"].Values["name"];

            Entity.Admin model = dal.GetModel<Entity.Admin>(CurrUID);

            IP = model.LastLoginIP;
            Time = model.LastLoginTime.ToString();
            LoginCount = model.LoginCount.ToString();
            AdminName = model.AdminName;
            AdminRole role = dal.GetModel<AdminRole>(model.RoleID);

            RoleName = role.RoleName;

            string whereStr = "";

            if (role.ID != 5 && role.ID != 2) //非采编或管理组
            {
                whereStr = "CreateUID=" + CurrUID;

            }

            if (!Page.IsPostBack)
            {

                if (Request.Params["action"] != null && Request.Params["action"].Trim() == "editpass")
                {

                   // MessageBox.ResponseScript(this, "editpass();");
                }
            
            }

          //  string action = Request.QueryString["action"];
            if(!string.IsNullOrEmpty(action))
            {
                if(action=="setsqlite")
                {
                    SetDB();
                }
            }





        }

        public void btnSave_Click(object sender, EventArgs e)
        {

            string PassWord = string.Empty;

            if (this.txtLoginName.Value == "")
            {
               // MessageBox.Show(this,"对比起,您的登录名有问题!");
                return;
            }
            if (this.txtAdminName.Value == "")
            {
               // MessageBox.Show(this,"对不起,您的姓名必须填写真实!");
                return;
            }

            if (this.txtOldPass.Value == "")
            {
               // MessageBox.Show(this, "必须输入正确的原始密码");
                return;
            }


            if ((this.txtPassWord.Value.Length > 0 || this.txtPassWord2.Value.Length > 0) && this.txtPassWord.Value != this.txtPassWord2.Value)
            {
               // MessageBox.Show(this, "两次密码必须输入一致!");
                return;
            }
            else
            {
                PassWord = this.txtPassWord2.Value; //有正确的新密码
            }
            string AdminName = this.txtAdminName.Value;
            int AdminID = int.Parse(Request.Cookies["eBlogAdmin"].Values["id"].ToString());
            string OldPass = MD5.Encrypt(this.txtOldPass.Value);
            Entity.Admin model = dal.GetModel<Entity.Admin>(AdminID);

            if (model.PassWord == OldPass)
            {
                model.AdminName = AdminName;

                if (!String.IsNullOrEmpty(PassWord))
                {
                    model.PassWord = MD5.Encrypt(PassWord);
                }
                model.TableName = "Admin";
                dal.Update<Entity.Admin>(model); //修改姓名或密码
              
                model = null;
                MessageBox.ShowAndRedirect(this, "密码修改成功!请重新登录!","login.aspx?action=quit");

                return;
            }
            else
            {

                MessageBox.Show(this, "对不起,您的原始密码错误!");
                return;
            }

        }

        /// <summary>
        /// 收缩SQLite数据库
        /// </summary>
        protected void SetDB()
        {
            dal.SetSQL("VACUUM");
            dal.ExecuteNonQuery();
        }
    }
}
